<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Akun extends CI_Controller {
    var $data_field;

	public function __construct()
	{
		parent::__construct();	
		$this->load->model('akun_model');
		$this->load->helper('flexigrid');
        $this->load->library('flexigrid');
		$this->load->library('session');
		$this->load->library('form_validation');
	}

	public function index()
	{	
		$this->loadDataGrid();
	}

     public function loadDataGrid(){
		$colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Kode_Akun'] = array('Nomer Akun', 120, TRUE, 'center', 1);
        $colModel['Nama_Akun'] = array('Nama Akun', 250, TRUE, 'center', 1);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
		
		 $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Akun',
            'showTableToggleBtn' => true
        );
		
		$buttons[] = array('Tambah', 'add', 'spt_js');
		
		$grid_js = build_grid_js('flex1', site_url("keuangan/akun/grid_akun"), $colModel, 'Kode_Akun', 'asc', $gridParams, $buttons);
		
		 $data['added_js'] =
         "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/keuangan/akun/tambah_akun'; 
			}
			
		} 
		</script>
		";
		
		$data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Akun';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'keuangan/grid_akun', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
	}
	
	function grid_akun() {
        $valid_fields = array('Kode_Akun','Nama_Akun');
        $this->flexigrid->validate_post('Nama_Akun', 'asc', $valid_fields);
        $records = $this->akun_model->get_data_akun();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
           

                $link_edit = "<a href='" . base_url() . "index.php/keuangan/akun/edit_akun/" . $row->ID_AKUN . "'>";
                $image_edit = "" . base_url() . "images/grid/edit.png";
                
        
            $record_items[] = array(
                $row->ID_AKUN,
                $no = $no + 1,
                $row->KODE_AKUN,
				$row->NAMA_AKUN,
                '' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> ',
               
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

        // fungsi untuk menampilkan form tambah akun
	public function tambah_akun()
	{
		$data['title'] = 'Tambah Akun';
                $data['post_action'] = 'keuangan/akun/insert_akun';

                $induk = $this->akun_model->get_akun_induk();

                $option_induk = null;
                foreach ($induk->result() as $row){
                    $option_induk[$row->ID_AKUN] = $row->NAMA_AKUN;
                }

                $data['option_induk'] = $option_induk;

		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'keuangan/tambah_akun',$data);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();
	}

        // fungsi untuk menampilkan form edit akun. @param id_akun
	public function edit_akun($id_akun)
	{
            if ($id_akun != NULL && $id_akun != ''){
			$data['title'] = 'Edit Akun';
                $data['post_action'] = "keuangan/akun/update_akun/".$id_akun;

                $akun = $this->akun_model->get_akun_byID($id_akun);
                if ($akun->num_rows() > 0){
                    $data['akun'] = $akun->row();
                    $induk = $this->akun_model->get_akun_induk();

                    $option_induk = null;
                    foreach ($induk->result() as $row){
                        $option_induk[$row->ID_AKUN] = $row->NAMA_AKUN;
                    }

                    $data['option_induk'] = $option_induk;

                    $this->template->write_view('header', 'template/header',$data);
                    $this->template->write_view('content', 'keuangan/edit_akun',$data);
                    $this->template->write_view('footer', 'template/footer',$data);
                    $this->template->render();
                }else
                    show_404('page');
            }else
                redirect("keuangan/akun");
	}

        // insert data akun ke db
        public function insert_akun(){
            if ($this->cek_validasi() == FALSE){
                $this->tambah_akun();
            }else{
                $this->load_data_form();
                $this->akun_model->tambah_akun($this->data_field);                

                redirect("keuangan/akun");
            }
        }

        // update data akun ke db. @param id_akun
        public function update_akun($id_akun){
            if ($id_akun != NULL && $id_akun != ''){
                if ($this->cek_validasi() == FALSE){
                    $this->edit_akun($id_akun);
                }else{
                    $this->load_data_form();
                    $this->akun_model->ubah_akun($this->data_field, $id_akun);

                    redirect("keuangan/akun");
                }
            }else
                show_404('page');
        }

        //load data inputan user dari form akun
	function load_data_form(){
            $nama_akun = $this->input->post('nama');
                $kode_akun = $this->input->post('kode_akun');
                $akun_induk = $this->input->post('akun_induk');
                $status = $this->input->post('status');
                $status_akun = ($akun_induk === 0 ? 1:0);

                if ($akun_induk === 0)
                    $level = 1;
                else {
                    $akun = $this->akun_model->get_akun_byID($akun_induk);
                    $level = $akun->row()->LEVEL_AKUN + 1;
                }

		$this->data_field = array(
                    'KODE_AKUN' => $kode_akun,
		   'NAMA_AKUN' => $nama_akun,
                    'ID_PARENT' => $akun_induk,
		   'LEVEL_AKUN' => $level,
                    'STATUS' => $status,
                    'STATUS_AKUN' => $status_akun
		);

	}

        // cek validasi setiap input user
        function cek_validasi() {
            $this->load->library('form_validation');
                $config = array(
                    //validasi form akun
                    array('field'=>'nama', 'label'=>'Nama Akun', 'rules'=>'required'),
                    array('field'=>'kode_akun','label'=>'Kode Akun', 'rules'=>'required|numeric|callback_cek_kode'),
                    array('field'=>'akun_induk', 'label'=>'Akun Induk', 'rules'=>'callback_cek_dropdown'),
                    array('field'=>'status', 'label'=>'Status', 'rules'=>''),
                );

                //setting rules
                $this->form_validation->set_rules($config);

		// Setting Error Message
		$this->form_validation->set_message('required', '%s harus diisi!!');
		$this->form_validation->set_message('numeric', '%s harus berisi angka !!');

		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

                return $this->form_validation->run();
        }

        //cek kode akun
	function cek_kode($kode){
            if ($kode != $this->input->post('kode_akun_hide')){
                $akun = $this->akun_model->get_akun_byKode($kode);
		if($akun->num_rows() > 0){
			$this->form_validation->set_message('cek_kode',
                            '%s sudah pernah dimasukkan');
			return FALSE;
		}else
			return TRUE;
            }else {
                return TRUE;
            }
	}

        //cek pilihan dropdown sdh bener ap blm
	function cek_dropdown($id_pilihan){
		if($id_pilihan==0){
			$this->form_validation->set_message('cek_dropdown',
                            'Harap memilih salah satu %s');
			return FALSE;
		}else
			return TRUE;
	}
}

/* End of file akun.php */
/* Location: ./application/controllers/akun.php */